package org.dftproject.genesis.ui.pages.summary.details;

import org.dftproject.genesis.data.genealogy.GenealogyUtils;
import org.dftproject.genesis.data.genealogy.IName;
import org.dftproject.genesis.data.genealogy.IPerson;
import org.dftproject.genesis.data.genealogy.Sex;
import org.dftproject.genesis.ui.SharedImages;
import org.dftproject.genesis.ui.figures.HyperlinkFigure;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.swt.widgets.Display;

public class PersonHyperlinkFigure extends HyperlinkFigure {

	public PersonHyperlinkFigure(final IPerson person) {
		super();
		
		setData(person);
		
		setText("(unknown)");
		setIcon(SharedImages.getDefault().getImage(SharedImages.IMG_NULL));

		Job job = new Job("Populate person") {

			@Override
			protected IStatus run(IProgressMonitor monitor) {
				final IName name = GenealogyUtils.getName(person);
				final Sex sex = GenealogyUtils.getSex(person);
				
				Display.getDefault().asyncExec(new Runnable() {

					public void run() {
						if (name != null)
							setText(GenealogyUtils.stringFromName(name));
						if (sex == Sex.Male)
							setIcon(SharedImages.getDefault().getImage(SharedImages.IMG_MALE));
						else if (sex == Sex.Female)
							setIcon(SharedImages.getDefault().getImage(SharedImages.IMG_FEMALE));
					}
					
				});
				
				return Status.OK_STATUS;
			}
			
		};
		job.schedule();
	}
	
}
